<template>
  <el-dialog :title="titleText" :visible="showDialog" width="50%" @close="btnCancel">
    <!-- 表单 -->
    <el-form ref="formRef" label-width="220px" :model="formData" :rules="rules">
      <el-form-item label="权限组名称">
        <!-- <el-radio v-model="formData.isPoint" label="false">菜单</el-radio>
        <el-radio v-model="formData.isPoint" label="true">权限点</el-radio>-->
        <!-- 修改 -->
        <el-radio-group v-if="formData.id" v-model="formData.is_point">
          <el-radio :label="false" disabled>菜单</el-radio>
          <el-radio :label="true" disabled>权限点</el-radio>
        </el-radio-group>
        <!-- 添加 -->
        <el-radio-group v-else v-model="formData.is_point">
          <el-radio :label="false">菜单</el-radio>
          <el-radio :label="true">权限点</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="权限父级" v-if="formData.is_point">
        <el-select v-model="formData.pid" placeholder="请选择" style="width:300px">
          <el-option label="主导航"/>
          <el-option-group
            v-for="group in menuList"
            :key="group.id"
            :label="group.title"
            class="sel-smallopt"
          >
            <el-option
              v-for="item in group.childs"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            />
          </el-option-group>
        </el-select>
      </el-form-item>
      <!-- 菜单 -->
      <el-form-item label="权限父级" v-else>
        <!-- <el-select v-model="formData.pid" placeholder="请选择"> -->
        <el-select
          :value="menuTitle"
          placeholder="请选择"
          @click.native="selectClick"
          id="selecmenuclick"
          style="width:300px"
        >
          <el-option v-show="isMneuShow" class="menutree">
            <el-tree
              :data="menuList"
              :props="defaultProps"
              default-expand-all
              :expand-on-click-node="false"
              @node-click="handleNodeClick"
            ></el-tree>
          </el-option>
        </el-select>
      </el-form-item>
      <!-- /菜单 -->
      <el-form-item label="权限代码" prop="code">
        <el-input v-model="formData.code" style="width:300px"/>
      </el-form-item>
      <el-form-item label="权限标题" prop="title">
        <el-input v-model="formData.title" style="width:300px"/>
      </el-form-item>
    </el-form>
    <!-- 底部确定 -->
    <template #footer>
      <el-row type="flex" justify="end">
        <el-button size="small" @click="btnCancel">取消</el-button>
        <el-button type="primary" size="small" @click="btnOk">确定</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { getMenusList, addMenu, getMenuById, editMenu } from '@/api/menus'
export default {
  name: 'OperatMenu',
  components: {},
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      isMneuShow: true,
      defaultProps: {
        children: 'childs',
        label: 'title'
      },
      menuTitle: '主导航',
      formData: {
        is_point: false,
        pid: '',
        code: '',
        title: ''
      },
      menuList: [],
      rules: {
        code: [{ required: true, message: '请输入权限代码', trigger: 'blur' }],
        title: [{ required: true, message: '请输入权限标题', trigger: 'blur' }]
      }
    }
  },
  computed: {
    // dialog标题计算属性
    titleText() {
      return this.formData.id ? '修改菜单' : '添加菜单'
    }
  },
  watch: {},
  created() {
    // this.getMenusList()
  },
  mounted() {},
  methods: {
    // 获取菜单列表
    async getMenusList() {
      try {
        const { data } = await getMenusList()
        this.menuList = data
        console.log(this.formData)
        this.$nextTick(() => {
          console.log(this.formData)
        })
        if (!this.formData.is_point) {
          this.menuList.unshift({ id: null, title: '主导航' })
        }
      } catch (err) {
        this.$message.error('获取菜单列表失败')
      }
    },
    // 点击select
    selectClick() {
      this.isMneuShow = true
    },
    // 菜单下拉触发点击时
    handleNodeClick(data) {
      document.getElementById('selecmenuclick').click()
      this.formData.pid = data.id
      this.menuTitle = data.title
      this.isMneuShow = false
    },
    // 确认添加按钮
    async btnOk() {
      this.$refs.formRef.validate(async isOk => {
        if (isOk) {
          try {
            if (this.formData.id) {
              await editMenu(this.formData)
            } else {
              await addMenu(this.formData)
            }
            this.$message.success(this.titleText + '成功')
            this.$emit('update:showDialog', false)
            this.$emit('addSuccess')
            this.getMenusList()
          } catch (err) {
            this.$message.error('失败')
          }
        }
      })
    },
    btnCancel() {
      this.$refs.formRef.resetFields()
      this.menuTitle = '主导航'
      this.formData = {
        is_point: false,
        pid: null,
        code: '',
        title: ''
      }
      this.$emit('update:showDialog', false)
    },
    // 获取菜单详情
    async getMenuById(id) {
      try {
        const { data } = await getMenuById(id)
        this.formData = data
        if (!data.is_point && data.pid !== null) {
          const res = await getMenusList()
          const result = res.data.filter(item => {
            if (item.id === data.pid) {
              return item
            }
          })
          this.menuTitle = result[0].title
        }
        return true
      } catch (err) {
        this.$message.error('获取详情失败')
      }
    }
  }
}
</script>
<style lang='scss' scoped>
.menutree {
  height: 150px;
  overflow-y: auto;
  font-size: 15px;
  padding: 0;
  background: #fff;
  padding-left: 20px;
  ::v-deep.el-tree {
    .el-tree-node__expand-icon {
      display: none;
    }
  }
}
.sel-option {
  margin-left: 15px;
  border: none;
}
.el-select-group__wrap {
  border: none;
}
</style>
